let questionDao = module.exports;
let util = require('util');
let mysqlPool = require('./MySQLPool');

questionDao.get = async function(id){
    let sql = 'select * from user_question where id = ? limit 1';
    let args = [id];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res[0];
    }

    return null;
};



questionDao.save = async function (question) {
    let sql = 'insert into user_question (uid, question, status, category, create_time) ' +
        ' values                         (?,   ?,        0,      0, now())';
    let args = [question.uid, question.question];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res.affectedRows;
    }

    return 0;
};

questionDao.updateById = async function(cond){
    let sql = 'update user_question set status = 1, answer = ? ' +
        ' where id = ?';
    let args = [cond.answer, cond.id];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res.affectedRows;
    }

    return 0;
};

questionDao.setReadById = async function(cond){
    let sql = 'update user_question set read_flag = 1 ' +
        ' where id = ? and uid = ?';
    let args = [cond.id, cond.uid];
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res.affectedRows;
    }

    return 0;
};

/**
 * @param cond
 *      {String} cond.uid
 *      {String} cond.startTime
 *      {String} cond.endTime
 *      {String} cond.page
 *      {String} cond.status
 */
questionDao.queryByCond = async function(cond) {
    if (!cond.page) cond.page = 1;
    if (!cond.count) cond.count = 10;
    cond.page = parseInt(cond.page);
    if (cond.page <= 0) cond.page = 1;
    cond.count = parseInt(cond.count);
    if (cond.count <= 0) cond.count = 10;

    let cntArgs = [];
    let cntSql = 'select count(1) cnt from user_question where 1=1 ';
    if (cond.uid){
        cntArgs.push(cond.uid);
        cntSql += ' and uid = ?';
    }

    if (cond.startTime) {
        cntArgs.push(cond.startTime + " 00:00:00");
        cntSql += ' and create_time >= ?';
    }

    if (cond.endTime) {
        cntArgs.push(cond.endTime + " 23:59:59");
        cntSql += ' and create_time <= ?';
    }

    if (cond.status){
        cntArgs.push(cond.status);
        cntSql += ' and status = ?';
    }

    let cntRes = await mysqlPool.query(cntSql, cntArgs);
    if (!cntRes) {
        return {cnt: 0, res:[]};
    }

    let sql = 'select * from user_question where 1=1 ';
    let args = [];
    if (cond.uid){
        args.push(cond.uid);
        sql += ' and uid = ?';
    }

    if (cond.startTime) {
        args.push(cond.startTime + " 00:00:00");
        sql += ' and create_time >= ?';
    }

    if (cond.endTime) {
        args.push(cond.endTime + " 23:59:59");
        sql += ' and create_time <= ?';
    }

    if (cond.status){
        args.push(cond.status);
        sql += ' and status = ?';
    }

    sql += ' order by create_time asc limit ?, ?';
    args.push((cond.page - 1) * cond.count);
    args.push(cond.count);
    let res = await mysqlPool.query(sql, args);
    if (res){
        return {cnt: cntRes[0].cnt, res:res};
    }

    return {cnt: 0, res:[]};
};

/**
 * @param cond
 *      {String} cond.uid
 */
questionDao.queryByUid = async function(uid) {
    let sql = 'select * from user_question where uid = ? and status = 1';
    let args = [uid];

    sql += ' order by create_time desc limit 30';
    let res = await mysqlPool.query(sql, args);
    if (res){
        return res;
    }

    return [];
};